﻿Imports System.Windows.Forms
Imports DotSpatial.Data, DotSpatial.Symbology

Public Class frmCreateRaster

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        With New SaveFileDialog
            '.InitialDirectory = GetSetting(REGAPP, Me.Name, "AddLayer", GISForm.LayerDirectory)
            .Filter = DotSpatial.Data.DataManager.DefaultDataManager.RasterWriteFilter
            .AddExtension = True
            .DefaultExt = ".bdg"
            .OverwritePrompt = True
            .Title = "Create Raster Layer"
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                With New clsRaster(GISForm.GetLayer(cboLayer.Text), cboField.Text, .FileName, txtCellSize.Text, udNumPts.Value, txtMaxDist.text)
                    Dim raster As Raster = .CreateRaster
                    raster.Save()
                End With

                GISForm.GISMap.AddLayer(.FileName)
                GISForm.GISLegend.Refresh()
            End If
        End With

        'Dim lyrRaster As New DotSpatial.Symbology.RasterLayer(raster)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub cboLayer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboLayer.SelectedIndexChanged
        If cboLayer.SelectedIndex <> -1 Then
            With cboField.Items
                .Clear()
                Dim fs As DotSpatial.Data.FeatureSet = GetFeatureSet(GISForm.GISMap, cboLayer.Text)
                For i As Integer = 0 To fs.DataTable.Columns.Count - 1
                    Dim colType As Type = fs.DataTable.Columns(i).DataType
                    Dim FieldName As String = fs.DataTable.Columns(i).ColumnName
                    If colType = GetType(System.Double) Or colType = GetType(System.Single) Or colType = GetType(System.Int32) Then .Add(FieldName)
                Next
                If cboField.Text = "" And cboField.Items.Count > 0 Then cboField.SelectedIndex = 0
            End With
        End If
    End Sub

    Private Sub frmCreateRaster_Load(sender As Object, e As EventArgs) Handles Me.Load
        With cboLayer.Items
            .Clear()
            For Each lyr In GISForm.GISMap.GetPolygonLayers
                If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
            Next
            For Each lyr In GISForm.GISMap.GetPointLayers
                If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
            Next
        End With
        If cboLayer.Items.Count > 0 Then cboLayer.SelectedIndex = 0
        udNumPts.Value = 5
        txtCellSize.Text = 50
    End Sub
End Class
